//
//  GroupModel.swift
//  Pods
//
//  Created by eme on 2017/4/5.
//
//

import Foundation
import SwiftyJSON
import ModelProtocol

class GroupModel: ModelProtocol {
    
    // MARK: Declaration for string constants to be used to decode and also serialize.
    internal let kGroupDataArrKey: String = "array"
    internal let kGroupPdnameKey: String = "pdname"
    internal let kGroupPdidKey: String = "pdid"
    internal let kGroupImg1Key: String = "img1"
    internal let kGroupPdtypeKey: String = "pdtype"
    internal let kGroupShpriceKey: String = "shprice"
    internal let kGroupDetailrnumberKey: String = "detailrnumber"
    internal let kGroupmoqKey: String = "moq"
    
    // MARK: 属性
    var dataArr: [DataArrModel]
    var pdname: String
    var pdid: Int
    var img1: String
    var pdtype: String
    var shprice: Double
    var detailrnumber: Int
    var moq:Int
    
    // MARK: 实现MikerSwiftJSONAble 协议， 解析json数据
    required  init?(json: JSON) {
        dataArr = []
        if let items = json[kGroupDataArrKey].array {
            for item in items {
                if let dataArrModel = DataArrModel(json: item){
                    dataArr.append(dataArrModel)
                }
            }
        } else {
            dataArr = []
        }
        pdname = json[kGroupPdnameKey].stringValue
        pdid = json[kGroupPdidKey].intValue
        img1 = json[kGroupImg1Key].stringValue
        pdtype = json[kGroupPdtypeKey].stringValue
        shprice = json[kGroupShpriceKey].doubleValue
        detailrnumber = json[kGroupDetailrnumberKey].intValue
        moq = json[kGroupmoqKey].intValue
    }
    
    
    /**
     Generates description of the object in the form of a NSDictionary.
     - returns: A Key value pair containing all valid values in the object.
     */
    internal func dictionaryRepresentation() -> [String : AnyObject ] {
        
        var dictionary: [String : AnyObject ] = [ : ]
        if dataArr.count > 0 {
            var temp: [AnyObject] = []
            for item in dataArr {
                temp.append(item.dictionaryRepresentation() as AnyObject)
            }
            dictionary.updateValue(temp as AnyObject, forKey: kGroupDataArrKey)
        
            dictionary.updateValue(pdname as AnyObject, forKey: kGroupPdnameKey)
        
            dictionary.updateValue(pdid as AnyObject, forKey: kGroupPdidKey)
    
            dictionary.updateValue(img1 as AnyObject, forKey: kGroupImg1Key)
        
            dictionary.updateValue(pdtype as AnyObject, forKey: kGroupPdtypeKey)
        
            dictionary.updateValue(shprice as AnyObject, forKey: kGroupShpriceKey)
        
            dictionary.updateValue(detailrnumber as AnyObject, forKey: kGroupDetailrnumberKey)
        
            dictionary.updateValue(moq as AnyObject, forKey: kGroupmoqKey)
        
        }
        return dictionary
    }
    
    
}
